//
//  BLMainViewController.swift
//  BLLiveTelecast
//
//  Created by BigL055 on 16/5/20.
//  Copyright © 2016年 bigL.liveTelecast.com. All rights reserved.
//

import Foundation
import UIKit

class BLTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //设置所有子控制器
        setupallChildViewController()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.navigationBarHidden = false
    }
    
    override func loadView() {
        
        super.loadView()
        //设置tableBar字体颜色
        if #available(iOS 9.0, *) {
            // 获取全局的tabBarItem
            let item = UITabBarItem.appearanceWhenContainedInInstancesOfClasses([self.classForCoder])
            // 颜色
            let attr = [NSForegroundColorAttributeName : bl_orangeColor]
            // 设置选中状态的tabBarButton
            item.setTitleTextAttributes(attr, forState: .Selected)
            
            let attrNor = [NSFontAttributeName : UIFont.systemFontOfSize(12)]
            item.setTitleTextAttributes(attrNor, forState: .Normal)
            
        } else {
            // Fallback on earlier versions
        }
    }
}


// MARK:- 设置所有子控制器

extension BLTabBarController{
    
    func setupallChildViewController(){
        
        setupSingleChildViewController(viewController: BLHomeViewController(),
                                       name: "首页",
                                       image: "btn_home_normal_24x24_",
                                       selectImage: "btn_home_selected_24x24_")
        
        
        setupSingleChildViewController(viewController: BLUserViewController(),
                                       name: "我的",
                                       image: "btn_user_normal_24x24_",
                                       selectImage: "btn_user_selected_24x24_")
    }
    
    
    /**
     包装单个子控制器
     
     - parameter viewController: 需要包装的子控制器
     - parameter name:           设置子控制器tabBarItem标题
     - parameter image:          设置子控制器tabBarItem 普通图片
     - parameter selectImage:    设置子控制器tabBarItem 选中图片
     */
    func setupSingleChildViewController(viewController  viewController : UIViewController,
                                                        name:String,
                                                        image:String,
                                                        selectImage:String){
        
        
        //  使用navigationController对控制器进行包装
        let navViewController = BLNavigationController(rootViewController: viewController)
        //  设置标题与Tabbar按钮文字
        viewController.title = name;
        viewController.tabBarItem.image = UIImage.bl_imageWithOriginalImageName(image)
        viewController.tabBarItem.selectedImage = UIImage.bl_imageWithOriginalImageName(selectImage)
        //  添加子控制器
        
        self.addChildViewController(navViewController)
    }
    
    
}







